package router

import (
	"chirp-read/handler"
	"chirp-read/handlers"
	"github.com/gin-gonic/gin"
	"net/http"
)

func Router() *gin.Engine {
	r := gin.Default()
	r.Use(Cors()) //默认跨域
	r.Static("/static", "./static")
	r.POST("/login", handlers.Login)
	r.GET("/list", handlers.List)
	r.GET("/detail", handlers.Detail)
	r.POST("/userRoleAdd", handlers.UserRoleAdd)
	r.POST("/roleNodeAdd", handlers.RoleNodeAdd)
	r.GET("/userRoleList", handlers.UserRoleList)
	r.GET("/roleNodeList", handlers.RoleNodeList)
	////////////////////
	r.POST("/depotAdd", handlers.DepotAdd)
	r.POST("/depotOrderAdd", handlers.DepotOrderAdd)
	r.GET("/depotOrderList", handlers.DepotOrderList)
	r.GET("/depotOrderDetail", handlers.DepotOrderDetail)
	///////////////////////
	r.POST("/goodAdd", handlers.GoodAdd)
	r.POST("/goodOrderAdd", handlers.GoodOrderAdd)
	r.GET("/goodOrderList", handlers.GoodOrderList)
	r.GET("/goodOrderDetail", handlers.GoodOrderDetail)
	///////////////////////
	r.POST("/courierAdd", handlers.CourierAdd)
	r.POST("/courierOrderAdd", handlers.CourierOrderAdd)
	r.GET("/courierOrderList", handlers.CourierOrderList)
	r.GET("/courierOrderDetail", handlers.CourierOrderDetail)
	///////////////////////////////////////////
	r.POST("users/login", handler.UsersLogin)
	r.POST("/users/usersRoleAdd", handler.UsersRoleAdd)
	r.POST("/users/roleNodeAdd", handler.RoleNodeAdd)
	r.GET("/users/usersRoleList", handler.UserRoleList)
	r.GET("/users/roleNodeList", handler.RoleNodeList)
	r.GET("/users/usersList", handler.UsersList)
	r.GET("/users/usersDetail", handler.UsersDetail)
	r.POST("/carAdd", handler.CarAdd)
	r.POST("/carOrderAdd", handler.CarOrderAdd)
	r.GET("/carOrderList", handler.CarOrderList)
	r.GET("/carOrderDetail", handler.CarOrderDetail)
	return r
}
func Cors() gin.HandlerFunc {
	return func(c *gin.Context) {
		method := c.Request.Method
		origin := c.Request.Header.Get("Origin")
		if origin != "" {
			c.Header("Access-Control-Allow-Origin", "*")
			c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
			c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization")
			c.Header("Access-Control-Allow-Credentials", "true")
			c.Set("content-type", "application/json")
		}
		//放行所有OPTIONS方法
		if method == "OPTIONS" {
			c.AbortWithStatus(http.StatusNoContent)
		}
		c.Next()
	}
}
